﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;


namespace community
{
    public partial class hypertensionpatient : Form
    {


        public static string hp_JMBH = "";//双击datagridview1的记录的居民编号
        public static string hp_SHMB = "";//双击datagridview1的记录的疾病名称
        SqlConnection cn;
        SqlCommand cm;
        SqlDataAdapter dt;
        DataSet ds;
        string sql = "select a.inhabitantcode_chr as 居民编号,a.residentname_chr as 居民姓名,a.communityname_chr as 所属社区,a.sex_chr as 居民性别,a.birth_dat as 出生日期,b.diseasename_vchr  as 疾病名称,b.happendate_dat as 患病时间 from T_CS_INHABITANTRECORD_NEW as a,t_cs_membermedical as b  where a.inhabitantcode_chr=b.inhabitantcode_chr and b.diseasename_vchr like '%高血压%' ";
        
        public hypertensionpatient()
        {
            InitializeComponent();
        }
        private void hy_check(string str)
        {
            cn = new SqlConnection(SQL.conStr);
            cn.Open();
            dt = new SqlDataAdapter(str, cn);
            ds = new DataSet();
            dt.Fill(ds);
            dataGridView1.DataSource = ds.Tables[0];
            cn.Close();
        }

        private void hypertensionpatient_Load(object sender, EventArgs e)
        {
            hy_check(sql);
            
        }

        private void button1_Click(object sender, EventArgs e)
        {
            sql = "select a.inhabitantcode_chr as 居民编号,a.residentname_chr as 居民姓名,a.communityname_chr as 所属社区,a.sex_chr as 居民性别,a.birth_dat as 出生日期,b.diseasename_vchr  as 疾病名称,b.happendate_dat as 患病时间 from T_CS_INHABITANTRECORD_NEW as a,t_cs_membermedical as b  where a.inhabitantcode_chr=b.inhabitantcode_chr and b.diseasename_vchr like '%高血压%' ";

            if ((checkBox2.Checked == true) && (textBox1.Text == ""))
            {
                MessageBox.Show("请输入检索的社区", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                return;
            }
            if ((checkBox1.Checked == true) && (dateTimePicker1.Text == "") || (dateTimePicker2.Text == ""))
            {
                MessageBox.Show("请选择患病时间", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                return;
            }
            if (checkBox2.Checked == true  &&  textBox1.Text != "")
                sql += " and a.communityname_chr='" + textBox1.Text.Trim() + "'";
            if (checkBox1.Checked == true  && dateTimePicker1.Text != "" && dateTimePicker2.Text != "")
                sql += " and (b.happendate_dat between '" + Convert.ToDateTime(dateTimePicker1.Text) + "' and  '" + Convert.ToDateTime(dateTimePicker2.Text) + "')";
            hy_check(sql);
        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox1.Checked == true)
            {
                dateTimePicker1.Enabled = true;
                dateTimePicker2.Enabled = true;
            }
            else
            {
                dateTimePicker1.Enabled = false;
                dateTimePicker2.Enabled = false;

            }
        }

        private void checkBox2_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox2.Checked == true)
            {
                textBox1.Enabled = true;
                textBox1.Focus();
            }
            else
            {
                textBox1.Enabled = false;
                textBox1.Text = "";
            }
        }

        private void textBox1_Click(object sender, EventArgs e)
        {
            dataGridView2.Visible = true;
            SqlConnection cn = new SqlConnection(SQL.conStr);
            cn.Open();
            string sqlStr = "select COMMUNITYCODE_CHR as 社区编号,COMMUNITYNAME_VCHR as 社区名称  from t_cs_community  where  CURRENTCS_INT=1";
            SqlDataAdapter dt = new SqlDataAdapter(sqlStr, cn);
            DataSet ds = new DataSet();
            dt.Fill(ds);
            this.dataGridView2.DataSource = ds.Tables[0];
            dataGridView2.ColumnHeadersHeight = 39;
            dataGridView2.Columns[0].Width = 200;
            dataGridView2.Columns[1].Width = 150;
            cn.Close();
        }

        private void dataGridView2_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex >= 0)
            {
                textBox1.Text = dataGridView2.Rows[e.RowIndex].Cells[1].Value.ToString();
                if (textBox1.Text != null)
                    dataGridView2.Visible = false;
            }
        }

        private void dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex >= 0)
            {
                hp_JMBH = dataGridView1.Rows[e.RowIndex].Cells[0].Value.ToString();
                hp_SHMB = dataGridView1.Rows[e.RowIndex].Cells[5].Value.ToString();
            }
            this.Close();
        }
    }
}
